nagios配置邮件告警

        目前 nagios 只能在浏览器上查看各个服务的状态,当某个机器宕掉或者某个服务宕掉时,我们是不知道的,因为我们不可能一直盯着服务器看。这时候,就需要用到警告系统了,让它自动化,当发现问题时及时通知到我们。下面配置使用发邮件的方式来实现告警。

        以下操作都在服务器上完成。

        首先定义发邮件接收者。

1
[root@nagios ~]# vim /etc/nagios/objects/contacts.cfg

        增加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
define contact{
contact_name 123
use generic-contact
alias yanyi
email 18983111118@189.cn
}
define contact{
contact_name 456
use generic-contact
alias aaa
email 89429541@qq.com
}
define contactgroup{
contactgroup_name common
alias common
members 123,456
}

        说明: contacts.cfg 里面既可以定义 user 也可以定义 group ,先定义两个 user 123 和 456 ,然后把这两个 user 加入到 common 组里面。等会发邮件就发给 common 组就可以了,那这样 18983111118@189.cn 和 89429541@qq.com 都会收到邮件。

        然后在需要告警的服务里面加上 contactgroup

1
[root@nagios ~]# vim /etc/nagios/conf.d/192.168.0.98.cfg

        针对 check_load 服务增加告警相关的配置

1
2
3
4
5
6
7
8
9
10
11
12
define service{
use generic-service
host_name 192.168.0.98
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
contact_groups common
notifications_enabled 1
notification_period 24x7
notification_options w,u,c,r
}

        说明:notifications_enabled 1 表示是否开启提醒功能。 1 为开启,0 为禁用。一般,这个选项会在主配置文件 (nagios.cfg) 中定义,效果相同。

        notification_period 24×7 表示发送提醒的时间段。非常重要的主机(服务)定义为问题发生,都不会发送提醒。

        notification_options:w,u,c,r 表示 service 的状态。w 为 waning,u 为 unknown,c 为 critical,r 为 recover(恢复了),类似的还有一个 host对应的状态:d,u,r d 状态为 DOWN,u 状态为 UNREACHABLE,r 状态恢复为 OK,需要加入到 host 的定义配置里。

        编辑完成配置文件后,需要重启 nagios 服务

1
[root@nagios ~]# service nagios restart